﻿using System;
using System.Collections.Generic;
using System.Linq;
using DevelopTool.Bll.LocalDbBlls;
using DevelopTool.Common;
using DevelopTool.Model.CodeModels;
using DevelopTool.Model.CommonModels;
using DevelopTool.Model.Enums;
using LiteDB;
using Sunny.UI;

namespace DevelopTool.Bll.CodeBlls
{
    /// <summary>
    /// 代码库操作
    /// </summary>
    public class CodeBll : BaseBll
    {
        public CodeBll()
        {
            using (var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var col = db.GetCollection<Code>();
                col.EnsureIndex(x => x.NodeName);
                col.EnsureIndex(x => x.NodePid);
                col.EnsureIndex(x => x.NodeType);
                col.EnsureIndex(x => x.CreateTime);
                col.EnsureIndex(x => x.Id);
            }
        }


        public List<Code> GetList(MenuNodeType nodeType = MenuNodeType.Unknown)
        {
            using (var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var models = db.GetCollection<Code>().FindAll();

                if(nodeType != MenuNodeType.Unknown)
                {
                    models = models.Where(it => it.NodeType == nodeType);
                }

                return models.OrderByDescending(it => it.CreateTime).ToList();
            }
        }


        /// <summary>
        /// 根据代码内容关键字查询到代码库id集合
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public List<Code> GetListByText(string text)
        {
            using (var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                List<Code> resultList = new List<Code>();


                var col = db.GetCollection<Code>();

                var list = col.Find(it => !string.IsNullOrEmpty(it.MainCodeText) && it.MainCodeText.GZipDecompress().IndexOf(text, StringComparison.OrdinalIgnoreCase) > -1).Select(it => it.Id).ToList();
                var list2 = col.Find(it => !string.IsNullOrEmpty(it.CustomCodeText) && it.CustomCodeText.GZipDecompress().IndexOf(text, StringComparison.OrdinalIgnoreCase) > -1).Select(it => it.Id).ToList();
                list.AddRange(list2);

                foreach (int codeId in list)
                {
                    if (resultList.Exists(it => it.Id == codeId)) continue;//不重复插入

                    resultList.Add(col.FindOne(it => it.Id == codeId && it.NodeType == MenuNodeType.Node));//只查询节点，不要目录
                }

                return resultList;
            }
        }


        /// <summary>
        /// 根据代码库id获取代码内容对象
        /// </summary>
        /// <param name="codeId"></param>
        /// <returns></returns>
        public Code GetById(int id)
        {
            using (var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var col = db.GetCollection<Code>();

                var code = col.FindOne(it => it.Id == id);

                //解压缩内容
                if (!string.IsNullOrEmpty(code.MainCodeText))
                    code.MainCodeText = code.MainCodeText.GZipDecompress();

                //解压缩内容
                if (!string.IsNullOrEmpty(code.CustomCodeText))
                    code.CustomCodeText = code.CustomCodeText.GZipDecompress();

                return code;

            }
        }


        /// <summary>
        /// 保存代码库
        /// </summary>
        /// <param name=></param>
        /// <returns></returns>
        public Code Save(Code code)
        {
            using (var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var col = db.GetCollection<Code>();

                //压缩内容
                if (!string.IsNullOrEmpty(code.MainCodeText))
                    code.MainCodeText = code.MainCodeText.GZipCompress();

                //压缩内容
                if (!string.IsNullOrEmpty(code.CustomCodeText))
                    code.CustomCodeText = code.CustomCodeText.GZipCompress();

                code.UpdateTime = DateTime.Now;
                col.Upsert(code);

                return col.FindById(code.Id);
            }
        }


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Delete(int id)
        {
            using (var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var codeCol = db.GetCollection<Code>();

                //查询是否有子节点，若有子节点，则不删除。
                if (codeCol.Exists(it => it.NodePid == id)) return false;

                //删除代码库
                var result = codeCol.Delete(id);

                return result;
            }
        }



        public void SetRemark(int id,string remark)
        {
            using(var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var col = db.GetCollection<Code>();

                var model = col.FindOne(it => it.Id == id);

                model.Remark = remark;
                model.UpdateTime = DateTime.Now;

                col.Update(model);
            }
        }

        public ManageMenu Get(int id)
        {
            using(var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                return db.GetCollection<Code>().FindOne(it => it.Id == id);
            }
        }

    }
}
